using System;
using UnityEngine;
using QFramework;

namespace ProjectSurvival
{
    public partial class SimpleAxe : ViewController
    {
        private float intervalTime = 0f;

        private void Update()
        {
            intervalTime += Time.deltaTime;
            if (intervalTime > 1f)
            {
                intervalTime = 0;
                Axe.InstantiateWithParent(transform)
                    .Show()
                    .Self((self) =>
                    {
                        var randomX = RandomUtility.Choose(-8, -3, -1, 3, 5, 8);
                        var randomY = RandomUtility.Choose(1, 3, 5, 8);
                        var _rigidbody = self.GetComponent<Rigidbody2D>();
                        _rigidbody.velocity = new Vector2(randomX, randomY);
                        self.OnTriggerEnter2DEvent(other =>
                        {
                            var hurtBox = other.GetComponent<HitHurtBox>();
                            if (hurtBox)
                            {
                                if (hurtBox.Owner.CompareTag("Enemy"))
                                {
                                    hurtBox.Owner.GetComponent<Enemy>().Hurt(2f);
                                }
                            }
                        }).UnRegisterWhenGameObjectDestroyed(self);
                        ActionKit.OnUpdate.Register(() =>
                        {
                            if (Player.Instance)
                            {
                                if (Player.Instance.transform.position.y - self.transform.position.y > 15f)
                                {
                                    self.gameObject.DestroySelfGracefully();
                                }
                            }
                        }).UnRegisterWhenGameObjectDestroyed(self);
                    });
            }
        }
    }
}